package com.ly.sy.controller;


import com.ly.sy.entity.Course;
import com.ly.sy.entity.Homework;
import com.ly.sy.entity.Video;
import com.ly.sy.service.HomeworkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liangyu
 * @since 2020-11-24
 */
@Controller
@RequestMapping(value = "/sy/homework",method = RequestMethod.POST)
public class HomeworkController {
    @Autowired
    private HomeworkService homeworkService;
    //    跳转
    @GetMapping("/gohomework")
    public String gohomework(@RequestParam Integer courseid , HttpSession session){
        List<Homework> homeworks = homeworkService.HomeworkBycourseId(courseid);
        Set<Integer> homeworkp =  new HashSet<>();
        session.removeAttribute("homeworkp");
        for (Homework Hp : homeworks) {
            homeworkp.add(Hp.getHwGroup());
        }
        System.out.println("++++++++++++++++++++++++++++++统计作业批次+++++++++++++++++++++++++++++++");
        System.out.println("Set:"+homeworkp);
        for (Integer str : homeworkp) {
            System.out.println("val:"+str);
        }
        if(!ObjectUtils.isEmpty(homeworkp)) {
            session.setAttribute("homeworkp", homeworkp);
        }
        session.setAttribute("homeworkList",homeworks);
        return "User/homework";
    }
    //显示对应批次的作业内容
    @GetMapping("/golearn")
    public String golearn(@RequestParam Integer g ,HttpSession session){
        Course coursesv = (Course) session.getAttribute("coursesv");
        List<Homework> homeworkByP = homeworkService.HomeworkBypAndCid(coursesv.getCourseId(), g);

        session.setAttribute("homeworkByP",homeworkByP);
        session.setAttribute("HP",g);
        return "/User/test";
    }
//    ajax异步校验答案正确性
    @GetMapping("/Answer")
    @ResponseBody
    public String Answer(
            @RequestParam String hwCon,
            @RequestParam Integer hwId
    ){
        System.out.println(hwCon+"+"+hwId);
        Homework homework = homeworkService.getById(hwId);
        if(hwCon.equals(homework.getHwAnswer())){

            System.out.println("true");

            return "true";
        }
        System.out.println("false");
        return "false";
    }


    //**********************************admin*************************************
//    跳转到添加作业界面
    @GetMapping("gobuilhomework")
    public String gobuilhomework(){
        return "/admin/homeworkadd";
    }
//    发布作业
    @PostMapping("builhomework")
    public String builhomework(
            @RequestParam Integer hwgroup,
            @RequestParam String hwnum,
            @RequestParam String textarea,
            @RequestParam String pA,
            @RequestParam String pB,
            @RequestParam String pC,
            @RequestParam String pD,
            @RequestParam String result,
            HttpSession session
    ){
        Course courseshow = (Course) session.getAttribute("courseshow");
        Homework homework = new Homework();
        homework.setCourseId(courseshow.getCourseId());
        homework.setHwGroup(hwgroup);
        homework.setHwNum(hwnum);
        homework.setHwTitle(textarea);
        homework.setHwCona(pA);
        homework.setHwConb(pB);
        homework.setHwConc(pC);
        homework.setHwCond(pD);
        homework.setHwAnswer(result);
        homeworkService.save(homework);
        System.out.println("作业发布成功");
        System.out.println(homework);
        return "/admin/courseview";
    }

    //根据courseId获取相关作业
    @GetMapping("homeworkList")
    @ResponseBody
    public String homeworkList(@RequestParam Integer courseid, HttpSession session){
        List<Homework> homeworklist = homeworkService.HomeworkBycourseId(courseid);
        session.setAttribute("courseOfH",homeworklist);
        session.setAttribute("couseidOfH",courseid);
        System.out.println("获取到了本课程相关作业");
        return "/sy/homework/courseOfhomework";
    }
    @GetMapping("courseOfhomework")
    public String courseOfhomework(){
        return "/admin/courseOfhomework";
    }

    @GetMapping("appendhomework")
    public String appendhomework(@RequestParam Integer courseid,
                                 @RequestParam Integer useroradmin,
                                 HttpSession session){
        session.setAttribute("couseidOfH",courseid);
        Course course = new Course();
        course.setCourseId(courseid);
        session.setAttribute("courseshow",course);
        session.setAttribute("useroradmin",useroradmin);
        return "admin/appendHomework";
    }
    //添加作业
    @GetMapping("inserthomework")
    public String inserthomework(
            @RequestParam Integer hwgroup,
            @RequestParam String hwnum,
            @RequestParam String textarea,
            @RequestParam String pA,
            @RequestParam String pB,
            @RequestParam String pC,
            @RequestParam String pD,
            @RequestParam String result,
            HttpSession session
    ){
        Course courseshow = (Course) session.getAttribute("courseshow");
        Homework homework = new Homework();
        homework.setCourseId(courseshow.getCourseId());
        homework.setHwGroup(hwgroup);
        homework.setHwNum(hwnum);
        homework.setHwTitle(textarea);
        homework.setHwCona(pA);
        homework.setHwConb(pB);
        homework.setHwConc(pC);
        homework.setHwCond(pD);
        homework.setHwAnswer(result);
        homeworkService.save(homework);
        System.out.println("作业发布成功");
        System.out.println(homework);
        Integer uora = (Integer) session.getAttribute("useroradmin");
        if (uora==0){
            List<Homework> homeworklist = homeworkService.HomeworkBycourseId(courseshow.getCourseId());
            session.setAttribute("courseOfH",homeworklist);
            return "redirect:/sy/homework/courseOfhomework";
        }else {
            return "redirect:/sy/syuser/syclass";
        }

    }
    //课程相关视频的搜索功能
    @GetMapping("search")
    @ResponseBody
    public List<Homework> search(@RequestParam Integer searchstr, HttpSession session){
        Integer courseId = (Integer) session.getAttribute("couseidOfH");
        List<Homework> homeworkList = homeworkService.homeworklikeLise(searchstr, courseId);
        return homeworkList;
    }
    //作业的删除
    @PostMapping("deletehomework")
    @ResponseBody
    public boolean deletehomework(Integer homeworkId){
        //TODO
        //这里有一个更新session中的数据的步骤没做
        return homeworkService.removeById(homeworkId);
    }
    //作业编辑、修改
    @GetMapping("goEditorhomework")
    @ResponseBody
    public String goEditorhomework(@RequestParam Integer homeworkid, HttpSession session){
        Homework editorhomework = homeworkService.getById(homeworkid);
        session.setAttribute("editorhomework",editorhomework);
        return "/sy/homework/EditorHomework";
    }
    @GetMapping("EditorHomework")
    public String EditorHomework(){
        return "/admin/EditorHomework";
    }
    //提交修改
    @PostMapping("updateHomeworkById")
    public String updateHomeworkById(
            @RequestParam("hwId") Integer hwId,
            @RequestParam("courseId") Integer courseId,
            @RequestParam("hwgroup") Integer hwgroup,
            @RequestParam("hwnum") String hwnum,
            @RequestParam("textarea") String textarea,
            @RequestParam("pA") String pA,
            @RequestParam("pB") String pB,
            @RequestParam("pC") String pC,
            @RequestParam("pD") String pD,
            @RequestParam("result") String result,
            HttpSession session
    ){
        Homework homework = new Homework();
        homework.setHwId(hwId);
        homework.setCourseId(courseId);
        homework.setHwGroup(hwgroup);
        homework.setHwNum(hwnum);
        homework.setHwTitle(textarea);
        homework.setHwCona(pA);
        homework.setHwConb(pB);
        homework.setHwConc(pC);
        homework.setHwCond(pD);
        homework.setHwAnswer(result);
        homeworkService.updateById(homework);
        session.setAttribute("editorhomework",homework);
        return "admin/EditorHomework";
    }
}

